package com.cn.wanxi.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.entity.Result;
import com.cn.wanxi.entity.UserEntity;
import com.cn.wanxi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller//定义控制层
public class LoginController {
    @Autowired
    private IUserService iUserService;
    @RequestMapping("/test")
    public void aa() {
        System.out.println(111);
    }
    @RequestMapping("/login")
    public void login(UserEntity userEntity, HttpServletRequest request, HttpServletResponse resp) throws IOException {
        userEntity.setSessionCode((String) request.getSession().getAttribute("code"));
        String result = iUserService.login(userEntity).getResult();
        if ("success".equals(result)) {
            request.getSession().setAttribute(userEntity.getUsername(), userEntity.getUsername());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);
    }
}
